import axios from "./ajax";
import type { ResDataType } from "./ajax";

export const getQuestionService = async (id: string): Promise<ResDataType> => {
  const url = `/api/question/${id}`;
  const data = await axios.get(url);
  return data;
};

export const createQuestionService = async (): Promise<ResDataType> => {
  const url = `/api/question`;
  const data = await axios.post(url);
  return data;
};

type SearchOption = {
  keyword: string;
  isStar: boolean;
  isDeleted: boolean;
  pageSize: number;
  page: number;
};

export type QuestionListResult = {
  total: number;
  list: ResDataType[];
};

export const getQuestionListService = async (
  options: Partial<SearchOption> = {}
): Promise<QuestionListResult> => {
  const url = `/api/question`;
  const data = (await axios.get(url, {
    params: options,
  })) as QuestionListResult;
  return data;
};

export const updateQuestionService = async (
  id: string,
  opts: { [key: string]: any }
): Promise<ResDataType> => {
  const url = `/api/question/${id}`;
  const data = await axios.patch(url, opts);
  return data;
};

export const duplicateQuestionService = async (
  id: string
): Promise<ResDataType> => {
  const url = `/api/question/duplicate/${id}`;
  const data = await axios.post(url);
  return data;
};

export const deleteQuestionsService = async (
  ids: string[]
): Promise<ResDataType> => {
  const url = `/api/question`;
  const data = await axios.delete(url, { data: { ids } });
  return data;
};
